<?php

namespace app\admin\controller\record;

use app\common\controller\Backend;
use app\common\model\Behavior;
use app\common\model\BehaviorCategory;
use think\Db;
use think\Exception;
use think\exception\PDOException;

class Teacher extends Backend
{
    /**
     * @var \app\common\model\BehaviorRecord
     */
    protected $model = null;
    protected $searchFields = 'name';
    protected $noNeedRight = [''];
    protected $relationSearch = true;
    protected $schoolLimit = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('BehaviorRecord');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model->alias('behavior_record')
                ->join('__BEHAVIOR_RECORD_DETAIL__ detail', 'detail.record_id = behavior_record.id')
                ->where($where)
                ->where('behavior_record.teacher_id', '>', 0)
                ->whereOr('detail.is_deduct', 1)
                ->count();

            $list = $this->model->alias('behavior_record')
                ->join('__BEHAVIOR_RECORD_DETAIL__ detail', 'detail.record_id = behavior_record.id')
                ->join('__BEHAVIOR_CATEGORY__ category', 'category.id = detail.behavior_category_id', 'left')
                ->where($where)
                ->where('behavior_record.teacher_id', '>', 0)
                ->whereOr('detail.is_deduct', 1)
                ->field('behavior_record.school_id,behavior_record.class_id,behavior_record.teacher_id,behavior_record.student_id,behavior_record.dorm_id,behavior_record.record_username,behavior_record.record_user_group,behavior_record.created_at,behavior_record.json_data,detail.id,detail.record_id,detail.behavior_category_type,detail.behavior_id,detail.behavior_name,detail.behavior_type,detail.unit_score,detail.count,detail.score,category.name behavior_category_name')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            if ($list) {
                foreach ($list as $key => $value) {
                    $list[$key]['behavior_type_text'] = isset(Behavior::$typeList[$value['behavior_type']]) ? Behavior::$typeList[$value['behavior_type']] : '';
                    $list[$key]['behavior_category_type_text'] = isset(BehaviorCategory::$typeList[$value['behavior_category_type']]) ? BehaviorCategory::$typeList[$value['behavior_category_type']] : '';
                }
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $model = model('BehaviorRecordDetail');
            $list = $model->where('id', 'in', $ids)->select();
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
